<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<table class="easyui-datagrid" id="itemList" title="商品列表" 
       data-options="singleSelect:false,collapsible:true,pagination:true,url:'/item/list',method:'get',pageSize:30,toolbar:toolbar">
    <thead>
        <tr>
        	<th data-options="field:'ck',checkbox:true"></th>
        	<th data-options="field:'id',width:60">商品ID</th>
            <th data-options="field:'title',width:200">商品标题</th>
            <th data-options="field:'cid',width:100">所属类目id</th>
            <th data-options="field:'sellPoint',width:100">租点</th>
            <th data-options="field:'price',width:120,align:'right',formatter:JIUSUZU.formatPrice">租价（单个单天）</th>
            <th data-options="field:'num',width:70,align:'right'">库存数量</th>
            <th data-options="field:'status',width:60,align:'center',formatter:JIUSUZU.formatItemStatus">状态</th>
            <th data-options="field:'created',width:130,align:'center',formatter:JIUSUZU.formatDateTime">创建日期</th>
            <th data-options="field:'updated',width:130,align:'center',formatter:JIUSUZU.formatDateTime">更新日期</th>
        </tr>
    </thead>
</table>
<div id="itemEditWindow" class="easyui-window" title="编辑商品" data-options="modal:true,closed:true,iconCls:'icon-save',href:'item-edit'" style="width:80%;height:80%;padding:10px;">
</div>
<script>

    function getSelectionsIds(){
    	var itemList = $("#itemList");
    	var sels = itemList.datagrid("getSelections");
    	var ids = [];
    	for(var i in sels){
    		ids.push(sels[i].id);
    	}
    	ids = ids.join(",");
    	return ids;
    }
    
    var toolbar = [{
        text:'新增',
        iconCls:'icon-add',
        handler:function(){
        	$(".tree-title:contains('新增商品')").parent().click();
        }
    },{
		text : '编辑',
		iconCls : 'icon-edit',
		handler : function() {
			var ids = getSelectionsIds();
			if (ids.length == 0) {
				$.messager.alert('提示', '必须选择一个商品才能编辑!');
				return;
			}
			if (ids.indexOf(',') > 0) {
				$.messager.alert('提示', '只能选择一个商品!');
				return;
			}

			$("#itemEditWindow").window({
				onLoad : function() {
					//回显数据
					var data = $("#itemList").datagrid("getSelections")[0];
					data.priceView = JIUSUZU.formatPrice(data.price);
					$("#itemeEditForm").form("load", data);

					// 加载商品描述
					$.getJSON('/rest/item/query/item/desc/' + data.id, function(_data) {
						if (_data.status == 200) {
							//UM.getEditor('itemeEditDescEditor').setContent(_data.data.itemDesc, false);
							itemEditEditor.html(_data.data.itemDesc);
						}
					});

					//加载商品规格
					$.getJSON('/rest/item/param/item/query/' + data.id, function(_data) {
						if (_data && _data.status == 200 && _data.data && _data.data.paramData) {
							$("#itemeEditForm .params").show();
							$("#itemeEditForm [name=itemParams]").val(_data.data.paramData);
							$("#itemeEditForm [name=itemParamId]").val(_data.data.id);

							//回显商品规格
							var paramData = JSON.parse(_data.data.paramData);

							var html = "<ul>";
							for ( var i in paramData) {
								var pd = paramData[i];
								html += "<li><table>";
								html += "<tr><td colspan=\"2\" class=\"group\">" + pd.group + "</td></tr>";

								for ( var j in pd.params) {
									var ps = pd.params[j];
									html += "<tr><td class=\"param\"><span>" + ps.k + "</span>: </td><td><input autocomplete=\"off\" type=\"text\" value='"+ps.v+"'/></td></tr>";
								}

								html += "</li></table>";
							}
							html += "</ul>";
							$("#itemeEditForm .params td").eq(1).html(html);
						}
					});

					JIUSUZU.init({
						"pics" : data.image,
						"cid" : data.cid,
						fun : function(node) {
							JIUSUZU.changeItemParam(node, "itemeEditForm");
						}
					});
				}
			}).window("open");
		}
	},{
        text:'删除',
        iconCls:'icon-cancel',
        handler:function(){
        	var ids = getSelectionsIds();
        	if(ids.length == 0){
        		$.messager.alert('提示','未选中商品!');
        		return ;
        	}
        	$.messager.confirm('确认','确定删除ID为 '+ids+' 的商品吗？',function(r){
        	    if (r){
        	    	var params = {"ids":ids};
                	$.post("/item/delete",params, function(data){
            			if(data.status == 200){
            				$.messager.alert('提示','删除商品成功!',undefined,function(){
            					$("#itemList").datagrid("reload");
            				});
            			}
            		});
        	    }
        	});
        }
    },'-',{//'-'可进行分组，不同组用|分隔
        text:'下架',
        iconCls:'icon-remove',
        handler:function(){
        	var ids = getSelectionsIds();
        	if(ids.length == 0){
        		$.messager.alert('提示','未选中商品!');
        		return ;
        	}
        	$.messager.confirm('确认','确定下架ID为 '+ids+' 的商品吗？',function(r){
        	    if (r){
        	    	var params = {"ids":ids};
                	$.post("/item/unshelve",params, function(data){
            			if(data.status == 200){
            				$.messager.alert('提示','下架商品成功!',undefined,function(){
            					$("#itemList").datagrid("reload");
            				});
            			}
            		});
        	    }
        	});
        }
    },{
        text:'上架',
        iconCls:'icon-remove',
        handler:function(){
        	var ids = getSelectionsIds();
        	if(ids.length == 0){
        		$.messager.alert('提示','未选中商品!');
        		return ;
        	}
        	$.messager.confirm('确认','确定上架ID为 '+ids+' 的商品吗？',function(r){
        	    if (r){
        	    	var params = {"ids":ids};
                	$.post("/item/putaway",params, function(data){
            			if(data.status == 200){
            				$.messager.alert('提示','上架商品成功!',undefined,function(){
            					$("#itemList").datagrid("reload");
            				});
            			}
            		});
        	    }
        	});
        }
    }];
</script>